//$URL: https://dasding.googlecode.com/svn/trunk/api/src/main/java/de/piratech/dasding/api/rest/UserIdResource.java $
//$Id: UserIdResource.java 81 2012-10-23 22:38:16Z amuthmann@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.couch;

import de.piratech.dasding.data.ActionCategory;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.DatabaseException;
import java.util.ArrayList;
import java.util.List;
import org.ektorp.CouchDbConnector;
import org.ektorp.support.CouchDbRepositorySupport;
import org.ektorp.support.GenerateView;

/**
 */
public class ActionCategoryDataImpl extends CouchDbRepositorySupport<ActionCategory> implements
        ActionCategoryData {

  public ActionCategoryDataImpl(CouchDbConnector pCouchDBConnector) {
    super(ActionCategory.class, pCouchDBConnector, "ActionCategory");
    initStandardDesignDocument();
  }

  @Override
  public ActionCategory createActionCategory(ActionCategory pActionCategory) {
    db.create(pActionCategory);
    return pActionCategory;
  }

  @Override
  public ActionCategory getActionCategory(String pId) {
    return get(pId);
  }

  @Override
  public void updateActionCategory(ActionCategory pActionCategory) {
    update(pActionCategory);
  }

  @Override
  public void deleteActionCategory(ActionCategory pActionCategory) throws DatabaseException {
    remove(pActionCategory);
  }

  @Override
  public List<ActionCategory> getCategories() {
    return new ArrayList<ActionCategory>(getAll());
  }

  @Override
  public List<ActionCategory> getVerifiedCategories() {
    return findByVerified(true);
  }

  @Override
  public List<ActionCategory> getNotVerifiedCategories() {
    return findByVerified(false);
  }

  // Build up a view which supports booelans
  @GenerateView
  public List<ActionCategory> findByVerified(boolean pVerified) {
    return new ArrayList<ActionCategory>(db.queryView(createQuery("by_verified")
            .includeDocs(true)
            .key(pVerified), type));
  }
}
